package com.glacier.yuyuan.gen.webapp.domain;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.meta.Table;
import com.glacier.common.core.domain.BaseEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * date 2023-01-31 16:24
 * 表信息
 *
 * @author glacier
 * @version 1.0
 */
public class GenTableInfo extends BaseEntity {

    private static final long serialVersionUID = -3983313979368185872L;
    /**
     * 数据源ID
     */
    private String sourceId;

    /**
     * 名称
     */
    private String name;

    /**
     * 表名
     */
    private String tableName;

    /**
     * 说明
     */
    private String tableComment;

    /**
     * 实体名称
     */
    private String entityName;

    /**
     * 包名
     */
    private String packageName;

    /**
     * sql 文件路径
     */
    private String sqlPath;

    /**
     * xml 文件路径
     */
    private String xmlPath;

    /**
     * html 文件路径
     */
    private String htmlPath;

    /**
     * js 文件路径
     */
    private String jsPath;

    /**
     * css 文件路径
     */
    private String cssPath;

    /**
     * 排序
     */
    private Integer sort;

    /**
     * 表字段信息
     */
    private List<GenTableField> genTableFields;

    /**
     * 数据源ID
     */
    private String sourceName;

    public GenTableInfo() {
    }

    public GenTableInfo(String id) {
        super(id);
    }

    public String getSourceId() {
        return sourceId;
    }

    public void setSourceId(String sourceId) {
        this.sourceId = sourceId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getTableComment() {
        return tableComment;
    }

    public void setTableComment(String tableComment) {
        this.tableComment = tableComment;
    }

    public String getEntityName() {
        return entityName;
    }

    public void setEntityName(String entityName) {
        this.entityName = entityName;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getSqlPath() {
        return sqlPath;
    }

    public void setSqlPath(String sqlPath) {
        this.sqlPath = sqlPath;
    }

    public String getXmlPath() {
        return xmlPath;
    }

    public void setXmlPath(String xmlPath) {
        this.xmlPath = xmlPath;
    }

    public String getHtmlPath() {
        return htmlPath;
    }

    public void setHtmlPath(String htmlPath) {
        this.htmlPath = htmlPath;
    }

    public String getJsPath() {
        return jsPath;
    }

    public void setJsPath(String jsPath) {
        this.jsPath = jsPath;
    }

    public String getCssPath() {
        return cssPath;
    }

    public void setCssPath(String cssPath) {
        this.cssPath = cssPath;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getSourceName() {
        return sourceName;
    }

    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    public List<GenTableField> getGenTableFields() {
        return Optional.ofNullable(genTableFields)
                .orElseGet(() -> genTableFields = new ArrayList<>());
    }

    public void setGenTableFields(List<GenTableField> genTableFields) {
        this.genTableFields = Optional.ofNullable(genTableFields)
                .orElseGet(ArrayList::new);
    }

    public GenTableInfo autoFill(Table table, Map<String, GenFieldType> fieldTypeMap) {
        // TODO 填充
        this.name = table.getComment();
        this.tableName = table.getTableName();
        this.tableComment = table.getComment();
        this.entityName = StrUtil.upperFirst(StrUtil.toCamelCase(table.getTableName()));
        this.genTableFields = table.getColumns()
                .stream()
                .map(column -> new GenTableField().autoFill(column, fieldTypeMap))
                .collect(Collectors.toList());
        return this;
    }

    @Override
    public String toString() {
        return "GenTableInfo{" +
                "sourceId='" + sourceId + '\'' +
                ", name='" + name + '\'' +
                ", tableName='" + tableName + '\'' +
                ", tableComment='" + tableComment + '\'' +
                ", entityName='" + entityName + '\'' +
                ", packageName='" + packageName + '\'' +
                ", sqlPath='" + sqlPath + '\'' +
                ", xmlPath='" + xmlPath + '\'' +
                ", htmlPath='" + htmlPath + '\'' +
                ", jsPath='" + jsPath + '\'' +
                ", cssPath='" + cssPath + '\'' +
                ", sort=" + sort +
                ", sourceName='" + sourceName + '\'' +
                ", id='" + id + '\'' +
                ", createBy='" + createBy + '\'' +
                ", createTime=" + createTime +
                ", updateBy='" + updateBy + '\'' +
                ", updateTime=" + updateTime +
                ", remark='" + remark + '\'' +
                '}';
    }


}
